package com.example.hidesms.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.hidesms.R;
import com.example.hidesms.dto.BlockSmsItem;

public class BlockSmsAdapter extends ArrayAdapter<BlockSmsItem>{
	
	private Context mContext;
	private ArrayList<BlockSmsItem> list;
	private LayoutInflater mInflater;
	
	public void setList(ArrayList<BlockSmsItem> list) {
		this.list = list;
	}

	public BlockSmsAdapter(Context context, int textViewResourceId, ArrayList<BlockSmsItem> list) {
		super(context, textViewResourceId, list);
		mContext= context;
		this.list = list;
		this.mInflater = LayoutInflater.from(context);
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.block_sms_item, null);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		final BlockSmsItem si = list.get(position);
		holder.tvNumber.setText(si.getNumber());
		holder.tvName.setText(si.getName());
		if (si.getBitmapImage()!=null) {
			Log.e("getView", "photo "+si.getBitmapImage());
			holder.imgPhoto.setImageBitmap(si.getBitmapImage());
		}else{
			holder.imgPhoto.setImageResource(R.drawable.ic_user);
		}
		
		return convertView;
	}
	
	
	private class ViewHolder{
		private TextView tvNumber, tvTime, tvName;
		private ImageView imgPhoto;
		public ViewHolder(View parent){
			tvNumber = (TextView) parent.findViewById(R.id.tvNumber);
			tvName= (TextView) parent.findViewById(R.id.tvName);
			imgPhoto = (ImageView) parent.findViewById(R.id.img_contact);
		}
	}

}
